# content of conftest.py
import smtplib
import pytest


@pytest.fixture(scope="module",
                params=["smtp.gmail.com", "mail.python.org"])
def smtp_connection(request):
    smtp_connection = smtplib.SMTP("smtp.gmail.com", 587, timeout=5)

    def fin():
        print("teardown smtp_connection")
        smtp_connection.close()

    request.addfinalizer(fin)
    # raise ValueError("Exception 123 raised")  # for demo purposes, fin will be called anyway
    return smtp_connection  # provide the fixture value


@pytest.fixture(scope="module")
def smtp_connection_request(request):
    # the smtpserver fixture is passed as an argument to the fixture function from `test_smtp_introspect_request.py`
    server = getattr(request.module, "smtpserver", "mail.python.org")
    smtp_connection = smtplib.SMTP(server, 587, timeout=5)

    def fin():
        print("teardown smtp_connection")
        print(f"finalizing {smtp_connection} {server}")
        smtp_connection.close()

    request.addfinalizer(fin)
    return smtp_connection  # provide the fixture value
